package com.itheima.service;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupMapper;
import com.itheima.dao.CheckItemMapper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckGroupEditVo;
import com.itheima.pojo.CheckItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    CheckGroupMapper checkGroupMapper;
    @Autowired
    CheckItemMapper checkItemMapper;

    @Override
    public void add(CheckGroup checkGroup) {
        //插入检查组的基本信息并返回主键
        checkGroupMapper.add(checkGroup);

        Integer checkGroupId = checkGroup.getId();
        List<Integer> checkitemIds = checkGroup.getCheckitemIds();

        setCheckGroupAndCheckItemBatch(checkitemIds,checkGroupId);
    }

    public void  setCheckGroupAndCheckItemBatch(List<Integer> checkitemIds,Integer checkGroupId){
        if(CollectionUtil.isNotEmpty(checkitemIds)){
            List<Map> params = new ArrayList<>();
            for (Integer checkitemId : checkitemIds) {
                //循环建立检查组和检查项的关系
                Map map = new HashMap();
                map.put("checkGroupId",checkGroupId);
                map.put("checkitemId",checkitemId);
                params.add(map);
            }

            //insert into t_checkgroup_checkitem (checkgroup_id,checkitem_id) values (23,28),(23,29),(23,30)
            checkGroupMapper.setCheckGroupAndCheckItemBatch(params);
        }
    }

    public void  setCheckGroupAndCheckItem(List<Integer> checkitemIds,Integer checkGroupId){
        if(CollectionUtil.isNotEmpty(checkitemIds)){
            for (Integer checkitemId : checkitemIds) {
                //循环建立检查组和检查项的关系
                Map map = new HashMap();
                map.put("checkGroupId",checkGroupId);
                map.put("checkitemId",checkitemId);
                checkGroupMapper.setCheckGroupAndCheckItem(map);
//                checkGroupMapper.setCheckGroupAndCheckItem(checkGroupId,checkitemId);
            }
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //告诉mybtais要分页
        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());

        List<CheckGroup> checkGroups = checkGroupMapper.findPage(queryPageBean.getQueryString());

        return new PageResult(page.getTotal(),checkGroups);
    }

    @Override
    public CheckGroupEditVo find4Edit(Integer id) {
        //根据id查询检查组基本信息
        CheckGroup checkGroup = checkGroupMapper.findById(id);

        //查询所有检查项
        List<CheckItem> checkItems = checkItemMapper.findAll();

        //查询上次勾选的检查项id集合
        List<Integer> ids = checkGroupMapper.findCheckItemIdsByCheckGroupId(id);

        CheckGroupEditVo vo = new CheckGroupEditVo();
        vo.setCheckGroup(checkGroup);
        vo.setCheckItemIds(ids);
        vo.setCheckItems(checkItems);
        return vo;
    }

    @Override
    public void edit(CheckGroup checkGroup) {
        Integer checkGroupId = checkGroup.getId();
        //更新检查组的基本信息
        checkGroupMapper.edit(checkGroup);
        //删除原来的关系，
        checkGroupMapper.deleteRelation(checkGroupId);
        //重新建立关系
        setCheckGroupAndCheckItemBatch(checkGroup.getCheckitemIds(),checkGroupId);
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupMapper.findAll();
    }
}
